﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Invoicing.AJAX
{
    /// <summary>
    /// ResourceInfo 的摘要说明
    /// </summary>
    public class ResourceInfo : IHttpHandler
    {
        private HttpRequest Req = HttpContext.Current.Request;
        private ZY.BLL.Tab_Power dal = new ZY.BLL.Tab_Power();
        public void ProcessRequest(HttpContext context)
        {
            if (Req["action"] != null)
            {
                string action = Req["action"].ToString();

                switch (action)
                {
                    case "add":
                        #region 执行添加操作
                        try
                        {

                            ZY.Model.Tab_Power addmodel = new ZY.Model.Tab_Power();
                            addmodel.po_createtime = DateTime.Now;
                            addmodel.po_lastime = DateTime.Now;
                            addmodel.po_ico = "";
                            addmodel.po_isroot = int.Parse(Req["pwstate"].ToString());
                            addmodel.po_name = Req["txtpWName"].ToString();
                            addmodel.po_note = Req["pwnote"].ToString();
                            addmodel.po_src = Req["txtpWPath"].ToString();
                            addmodel.po_state = int.Parse(Req["pwstate"].ToString());
                            addmodel.po_type = int.Parse(Req["potype"].ToString());
                            addmodel.po_upid = int.Parse(Req["pwupid"].ToString());
                            dal.Add(addmodel);
                            context.Response.Write(Pubs.AjaxHandle.AjaxPress("200", "添加成功!", "resource", "Resources/ResourceManager.aspx"));

                        }
                        catch (Exception ex)
                        {
                            context.Response.Write(Pubs.AjaxHandle.AjaxPress("300", ex.Message));

                        }
                        #endregion
                        break;
                    case "edit":
                        #region  执行修改操作
                        if (Req["rid"]!=null)
                        {

                            try
                            {
                                ZY.Model.Tab_Power upmodel = dal.GetModel(int.Parse(Req["rid"].ToString()));
                                upmodel.po_name = Req["txtpWName"].ToString();
                                upmodel.po_lastime = DateTime.Now;
                                upmodel.po_note = Req["pwnote"].ToString();
                                upmodel.po_type = int.Parse(Req["potype"].ToString());
                                upmodel.po_state = int.Parse(Req["pwstate"].ToString());
                                upmodel.po_src = Req["txtpWPath"].ToString();
                                upmodel.po_isroot = int.Parse(Req["pwisroot"].ToString());
                                upmodel.po_upid = int.Parse(Req["pwupid"].ToString());
                                dal.Update(upmodel);
                                context.Response.Write(Pubs.AjaxHandle.AjaxPress("200", "修改成功!", "resource", "Resources/ResourceManager.aspx"));

                            }
                            catch (Exception ex)
                            {
                                context.Response.Write(Pubs.AjaxHandle.AjaxPress("300", ex.Message));
                            }
                        }
                        #endregion
                        break;
                    case "del":
                        #region 执行删除操作
                        string ids = Req["ids"].ToString();
                        string[] param = ids.Split(',');

                        foreach (var item in param)
                        {

                            try
                            {
                                if (item != null)
                                {

                                    dal.Delete(int.Parse(item));

                                }
                            }
                            catch (Exception ex)
                            {

                                context.Response.Write(Pubs.AjaxHandle.AjaxPress("300",ex.Message));

                            }

                        }

                        context.Response.Write(Pubs.AjaxHandle.AjaxPressTodo("200", "删除成功"));

                        #endregion 
                        break;
                    default:
                        break;
                }

            }
            else {

                context.Response.Write(Pubs.AjaxHandle.AjaxPress("300","非法操作"));
            }
                

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}